package j2024.j202407;

import java.util.*;

public class j0719 {

    public int LastRemaining_Solution (int n, int m) {
        // write code here
        if(n==0 || m==0){
            return -1;
        }
        //m,m+1,m+2
        //0,1,2
        return (LastRemaining_Solution(n-1,m)+m)%n;
    }
    int[] dx = {0,0,-1,1};
    int[] dy = {1,-1,0,0};
    public  int rotApple1 (ArrayList<ArrayList<Integer>> grid) {
        // write code here
        //多源bfs解决

        int m = grid.size();
        int n = grid.get(0).size();
        boolean ret = false;
        for (int i = 0; i < m; i++) {
            if(grid.get(i).contains(1)){
                ret = true;
            }
        }
        if(ret==false){
            return 0;
        }
        Queue<int[]> q = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(grid.get(i).get(j)==2){
                    q.add(new int[]{i,j});
                }
            }
        }
        int[][] arr = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j] = grid.get(i).get(j);
            }
        }
        //记录步数
        int step = 0;
        while(!q.isEmpty()){
            step++;
            int sz = q.size();
            while(sz-->0){
                int[] tmp = q.poll();
                int a =  tmp[0],b = tmp[1];
                for (int i = 0; i < 4; i++) {
                    int x = a+dx[i],y = b+dy[i];
                    if(x>=0 && x<m && y>=0 && y<n && arr[x][y]==1){
                        q.add(new int[]{x,y});
                        arr[x][y] = 2;
                    }
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(arr[i][j]==1){
                    return -1;
                }
            }
        }
        return step-1;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        ArrayList<ArrayList<Integer>> grid = new ArrayList<>();
        ArrayList<Integer> a = new ArrayList<>();
        a.add(2);a.add(1);a.add(1);

        ArrayList<Integer> b = new ArrayList<>();
        b.add(1);b.add(0);b.add(1);
        ArrayList<Integer> c = new ArrayList<>();
        c.add(1);
        c.add(1);
        c.add(1);
        grid.add(a);
        grid.add(b);
        grid.add(c);
        //{{2,1,1},{1,0,1},{1,1,1}}

    }
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        int[][] arr = new int[q][3];
        for (int i = 0; i < q; i++) {
            for (int j = 0; j < 3; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        int count = 0;
        int min = Integer.MAX_VALUE;

        for (int i = 0; i < q; i++) {
            for (int j = 0; j < 3; j++) {
                min = Math.min(min,arr[i][j]);
            }
            count+=min*2;
            int o =arr[i][1]-min;
            if(o-1>0){
                count+=(o-1);
            }
            System.out.println(count);
            min = Integer.MAX_VALUE;
            count = 0;
        }
    }
    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length-1;
        int max = 0;
        while(left<right){
            int tmp = (right-left)*Math.min(height[left],height[right]);
            max = Math.max(max,tmp);
            if(height[left]>height[right]){
                right--;
            }else {
                left++;
            }
        }
        return max;
    }
}
